﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.HVAC
{
	public partial class FrmGroupValue : System.Windows.Forms.Form
	{
		[DllImport("user32.dll", CharSet = CharSet.Auto)]
		private static extern int SendMessage(IntPtr Handle, int wMsg, int wParam, int lParam);

		public int Distance { get; set; }

		public List<FamilySymbol> LayFamilySymbol { get; set; }

		public static void SetListViewSpacing(ListView lst, int x, int y)
		{
			FrmGroupValue.SendMessage(lst.Handle, 4149, 0, x * 65536 + y);
		}

		public FrmGroupValue()
		{
			this.InitializeComponent();
		}

		public FrmGroupValue(Document document, int nNumMax = 0)
		{
			this.InitializeComponent();
			this.doc = document;
			//base.Icon = Resource.YArchitechIcon;
			this.nValveNumMax = nNumMax;
			if (this.nValveNumMax > 0)
			{
				this.label2.Visible = true;
				this.label2.Text = string.Format("组合阀门(最多允许同时布置{0}个阀门)", this.nValveNumMax);
			}
		}

		private void FrmGroupValue_Load(object sender, EventArgs e)
		{
			Size imageSize = this.listView1.LargeImageList.ImageSize;
			FrmGroupValue.SetListViewSpacing(this.listView1, imageSize.Width + 30, imageSize.Height + 10);
			imageSize = this.listView2.LargeImageList.ImageSize;
			FrmGroupValue.SetListViewSpacing(this.listView2, imageSize.Width, imageSize.Height);
			this.GetAllFamily();
			this.GetUserData();
			this.listView2_SelectedIndexChanged(null, null);
		}

		public void IsShowLayType(bool bShow)
		{
            System.Windows.Forms.Control control = this.radSelPoint;
            System.Windows.Forms.Control control2 = this.radFromPoint;
			this.hyTextBox1.Visible = bShow;
			control2.Visible = bShow;
			control.Visible = bShow;
			this.btnLay.Text = "确定";
		}

		private void GetAllFamily()
		{
			SpaceFamilyDataReader spaceFamilyDataReader = new SpaceFamilyDataReader();
			this.listFamilyName = spaceFamilyDataReader.GetAllNameAndImage(this.pipeValveTableName);
			this.listFamilyName = (from c in this.listFamilyName
			orderby c.Name
			select c).ToList<ListItem>();
			this.FilterValue();
			this.BandListView(this.listFamilyName, this.listView1);
		}

		private void FilterValue()
		{
			SpaceFamilyDataReader spaceFamilyDataReader = new SpaceFamilyDataReader();
			for (int i = this.listFamilyName.Count - 1; i >= 0; i--)
			{
				ListItem listItem = this.listFamilyName[i];
				foreach (ListItem listItem2 in spaceFamilyDataReader.GetInfoById(this.pipeValveTableName, Convert.ToInt32(listItem.Tag.ToString())))
				{
					if (listItem2.Name.Equals("IsCombin") && Convert.ToInt32(listItem2.Value) != 1)
					{
						this.listFamilyName.RemoveAt(i);
					}
				}
			}
		}

		private void BandListView(List<ListItem> listDBFamily, ListView listView)
		{
			listView.Items.Clear();
			listView.LargeImageList.Images.Clear();
			listView.LargeImageList.ImageSize = new Size(80, 80);
			foreach (ListItem listItem in listDBFamily)
			{
				listView.LargeImageList.Images.Add(listItem.Name, Image.FromFile(listItem.Value));
				ListViewItem listViewItem = new ListViewItem();
				listViewItem.Text = listItem.Name;
				listViewItem.Tag = listItem;
				listViewItem.ImageKey = listItem.Name;
				listView.Items.Add(listViewItem);
			}
			listView.Items[0].Selected = true;
		}

		private void GetUserData()
		{
			UserSetting userSetting = new UserSetting();
			if (Convert.ToInt32(userSetting.GetValue("FrmGroupValue", "InsertType")) == 1)
			{
				this.radSelPoint.Checked = true;
				this.hyTextBox1.Enabled = false;
			}
			else
			{
				this.radFromPoint.Checked = true;
				this.hyTextBox1.Enabled = true;
			}
			this.hyTextBox1.Text = userSetting.GetValue("FrmGroupValue", "DisFromPoint");
			string value = userSetting.GetValue("FrmGroupValue", "ComValueName");
			this.InitCombValve(value);
			if (this.nValveNumMax > 0)
			{
				return;
			}
			value = userSetting.GetValue("FrmGroupValue", "DefValve");
			foreach (string value2 in value.Split(new char[]
			{
				','
			}))
			{
				if (!string.IsNullOrEmpty(value2))
				{
					foreach (ListItem listItem in this.listFamilyName)
					{
						if (listItem.Tag.ToString().Equals(value2))
						{
							this.AddInList2View(listItem);
						}
					}
				}
			}
		}

		private void SetUserData()
		{
			UserSetting userSetting = new UserSetting();
			if (this.radSelPoint.Checked)
			{
				userSetting.WriteValue("FrmGroupValue", "InsertType", "1");
			}
			else
			{
				userSetting.WriteValue("FrmGroupValue", "InsertType", "2");
			}
			userSetting.WriteValue("FrmGroupValue", "DisFromPoint", this.hyTextBox1.Text);
			userSetting.WriteValue("FrmGroupValue", "ComValueName", this.GetComValueNameStr());
			if (this.nValveNumMax > 0)
			{
				return;
			}
			string text = "";
			foreach (object obj in this.listView2.Items)
			{
				ListItem listItem = ((ListViewItem)obj).Tag as ListItem;
				text = text + "," + listItem.Tag.ToString();
			}
			userSetting.WriteValue("FrmGroupValue", "DefValve", text);
		}

		private void btnImgAdd_Click(object sender, EventArgs e)
		{
			if (this.listView1.SelectedItems.Count < 1)
			{
				return;
			}
			this.AddInList2View(this.listView1.SelectedItems[0].Tag as ListItem);
		}

		private void AddInList2View(ListItem itemFamily)
		{
			if (itemFamily == null)
			{
				return;
			}
			if (this.nValveNumMax > 0 && this.listView2.Items.Count >= this.nValveNumMax)
			{
				return;
			}
			this.listView2.LargeImageList.Images.Add(itemFamily.Name, Image.FromFile(itemFamily.Value));
			ListViewItem listViewItem = new ListViewItem();
			listViewItem.Text = itemFamily.Name;
			listViewItem.Tag = itemFamily;
			listViewItem.ImageKey = itemFamily.Name;
			this.listView2.Items.Add(listViewItem);
			listViewItem.Selected = true;
		}

		private void btnImgDel_Click(object sender, EventArgs e)
		{
			this.DelItemFromListView(this.listView2);
			this.listView2_SelectedIndexChanged(null, null);
		}

		private void DelItemFromListView(ListView view)
		{
			if (view.SelectedItems.Count < 1)
			{
				return;
			}
			int num = view.SelectedItems[0].Index;
			view.Items.Remove(view.SelectedItems[0]);
			view.Refresh();
			if (view.Items.Count > num)
			{
				view.Items[num].Selected = true;
				return;
			}
			if (view.Items.Count == num && num > 0)
			{
				num--;
				num = ((num >= 0) ? num : 0);
				view.Items[num].Selected = true;
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btnLay_Click(object sender, EventArgs e)
		{
			if (this.listView2.Items.Count <= 0)
			{
				YJKMessageBox.Error("组合阀门内容不能为空！");
				return;
			}
			if (this.nValveNumMax > 0 && this.listView2.Items.Count > this.nValveNumMax)
			{
				YJKMessageBox.Error(string.Format("组合阀门最多允许同时布置{0}个阀门！", this.nValveNumMax));
				return;
			}
			if (long.Parse(this.hyTextBox1.Text) > 100000L || int.Parse(this.hyTextBox1.Text) < 100)
			{
				YJKMessageBox.Error("插入点距水管端点距离应为100mm~100000mm之间！");
				return;
			}
			SpaceFamilyDataReader spaceFamilyDataReader = new SpaceFamilyDataReader();
			this.LayFamilySymbol = new List<FamilySymbol>();
			this.LayFamilySymbol.Clear();
			foreach (object obj in this.listView2.Items)
			{
				ListItem listItem = ((ListViewItem)obj).Tag as ListItem;
				FamilySymbol familySymbolByIdSubTran = spaceFamilyDataReader.GetFamilySymbolByIdSubTran(Convert.ToInt32(listItem.Tag.ToString()), this.pipeValveTableName, null, this.doc, BuiltInCategory.OST_PipeAccessory);
				if (familySymbolByIdSubTran != null)
				{
					this.LayFamilySymbol.Add(familySymbolByIdSubTran);
				}
			}
			this.Distance = Convert.ToInt32(this.hyTextBox1.Text);
			this.nLayType = (this.radSelPoint.Checked ? 1 : 2);
			this.SetUserData();
			base.DialogResult = DialogResult.OK;
		}

		private void btn_Clean_Click(object sender, EventArgs e)
		{
			this.listView2.Items.Clear();
			this.listView2.Refresh();
			this.listView2_SelectedIndexChanged(null, null);
		}

		private void btn_Left_Click(object sender, EventArgs e)
		{
			if (this.listView2.SelectedItems == null)
			{
				return;
			}
			int index = this.listView2.SelectedItems[0].Index;
			if (index <= 0)
			{
				return;
			}
			List<ListViewItem> list = new List<ListViewItem>();
			foreach (object obj in this.listView2.Items)
			{
				ListViewItem item = (ListViewItem)obj;
				list.Add(item);
			}
			list[index] = this.listView2.Items[index - 1];
			list[index - 1] = this.listView2.Items[index];
			this.listView2.Items.Clear();
			foreach (ListViewItem value in list)
			{
				this.listView2.Items.Add(value);
			}
			this.listView2_SelectedIndexChanged(null, null);
		}

		private void btn_Right_Click(object sender, EventArgs e)
		{
			if (this.listView2.SelectedItems == null)
			{
				return;
			}
			int index = this.listView2.SelectedItems[0].Index;
			if (index >= this.listView2.Items.Count - 1)
			{
				return;
			}
			List<ListViewItem> list = new List<ListViewItem>();
			foreach (object obj in this.listView2.Items)
			{
				ListViewItem item = (ListViewItem)obj;
				list.Add(item);
			}
			list[index] = this.listView2.Items[index + 1];
			list[index + 1] = this.listView2.Items[index];
			this.listView2.Items.Clear();
			foreach (ListViewItem value in list)
			{
				this.listView2.Items.Add(value);
			}
			this.listView2_SelectedIndexChanged(null, null);
		}

		private void listView2_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.listView2.Items.Count <= 0)
			{
				this.btn_Clean.Enabled = false;
				this.btn_comAdd.Enabled = false;
				this.btn_Removeout.Enabled = false;
				this.btn_Left.Enabled = false;
				this.btn_Right.Enabled = false;
			}
			else
			{
				if (this.listView2.SelectedItems.Count == 0)
				{
					this.btn_Removeout.Enabled = false;
					this.btn_Left.Enabled = false;
					this.btn_Right.Enabled = false;
				}
				else
				{
					int index = this.listView2.SelectedItems[0].Index;
					if (index == 0)
					{
						this.btn_Left.Enabled = false;
						this.btn_Right.Enabled = true;
					}
					else if (index == this.listView2.Items.Count - 1)
					{
						this.btn_Left.Enabled = true;
						this.btn_Right.Enabled = false;
					}
					else
					{
						this.btn_Left.Enabled = true;
						this.btn_Right.Enabled = true;
					}
					if (this.listView2.Items.Count == 1)
					{
						this.btn_Left.Enabled = false;
						this.btn_Right.Enabled = false;
					}
					this.btn_Removeout.Enabled = true;
				}
				this.btn_Clean.Enabled = true;
				this.btn_comAdd.Enabled = true;
			}
			if (this.listViewCombin.Items.Count <= 0 || this.listViewCombin.SelectedItems.Count <= 0)
			{
				this.btn_ComDel.Enabled = false;
				this.btn_ComLeft.Enabled = false;
				return;
			}
			this.btn_ComDel.Enabled = true;
			this.btn_ComLeft.Enabled = true;
		}

		private void btn_comAdd_Click(object sender, EventArgs e)
		{
			List<ListItem> selValues = this.GetSelValues();
			if (selValues == null)
			{
				YJKMessageBox.Error("阀门组合中不能为空！");
				return;
			}
			string text = "常用阀门组合";
			int num = 1;
			string text2;
			foreach (object obj in this.listViewCombin.Items)
			{
				text2 = ((ListViewItem)obj).Text;
				if (text2.IndexOf(text) > -1 && Convert.ToInt32(text2.Substring(6)) >= num)
				{
					num = Convert.ToInt32(text2.Substring(6)) + 1;
				}
			}
			text2 = text + num.ToString();
			ListViewItem listViewItem = new ListViewItem();
			listViewItem.Text = text2;
			listViewItem.Tag = selValues;
			this.listViewCombin.Items.Add(listViewItem);
		}

		private void InitCombValve(string szComb)
		{
			if (string.IsNullOrEmpty(szComb.Trim()))
			{
				return;
			}
			foreach (string text in szComb.Split(new char[]
			{
				'|'
			}))
			{
				if (!string.IsNullOrEmpty(text))
				{
					string[] array2 = text.Split(new char[]
					{
						':'
					});
					ListViewItem listViewItem = new ListViewItem();
					listViewItem.Text = array2[0];
					listViewItem.Tag = this.GetSelValues(array2[1]);
					this.listViewCombin.Items.Add(listViewItem);
				}
			}
			if (this.listViewCombin.Items.Count >= 1)
			{
				this.listViewCombin.Items[0].Selected = true;
			}
		}

		private List<ListItem> GetSelValues()
		{
			if (this.listView2.Items.Count <= 0)
			{
				return null;
			}
			List<ListItem> list = new List<ListItem>();
			foreach (object obj in this.listView2.Items)
			{
				ListViewItem listViewItem = (ListViewItem)obj;
				list.Add(listViewItem.Tag as ListItem);
			}
			if (list.Count > 0)
			{
				return list;
			}
			return null;
		}

		private List<ListItem> GetSelValues(string szValveAry)
		{
			if (string.IsNullOrEmpty(szValveAry))
			{
				return null;
			}
			string[] array = szValveAry.Split(new char[]
			{
				','
			});
			List<ListItem> list = new List<ListItem>();
			foreach (string value in array)
			{
				foreach (ListItem listItem in this.listFamilyName)
				{
					if (listItem.Tag.ToString().Equals(value))
					{
						list.Add(listItem);
					}
				}
			}
			if (list.Count > 0)
			{
				return list;
			}
			return null;
		}

		private string GetComValueNameStr()
		{
			string text = null;
			foreach (object obj in this.listViewCombin.Items)
			{
				ListViewItem listViewItem = (ListViewItem)obj;
				string text2 = "|" + listViewItem.Text + ":";
				foreach (ListItem listItem in (listViewItem.Tag as List<ListItem>))
				{
					text2 = text2 + listItem.Tag.ToString() + ",";
				}
				text += text2;
			}
			return text;
		}

		private void btn_ComLeft_Click(object sender, EventArgs e)
		{
			if (this.listViewCombin.SelectedItems.Count <= 0)
			{
				YJKMessageBox.Error("请选择阀门组合！");
				return;
			}
			this.btn_Clean_Click(null, null);
			foreach (ListItem listItem in (this.listViewCombin.SelectedItems[0].Tag as List<ListItem>))
			{
				if (this.nValveNumMax > 0 && this.listView2.Items.Count >= this.nValveNumMax)
				{
					break;
				}
				this.listView2.LargeImageList.Images.Add(listItem.Name, Image.FromFile(listItem.Value));
				ListViewItem listViewItem = new ListViewItem();
				listViewItem.Text = listItem.Name;
				listViewItem.Tag = listItem;
				listViewItem.ImageKey = listItem.Name;
				this.listView2.Items.Add(listViewItem);
				listViewItem.Selected = true;
			}
		}

		private void btn_ComDel_Click(object sender, EventArgs e)
		{
			if (this.listViewCombin.SelectedItems.Count <= 0)
			{
				YJKMessageBox.Error("请选择要删除的阀门组合！");
				return;
			}
			this.DelItemFromListView(this.listViewCombin);
		}

		private void listViewCombin_AfterLabelEdit(object sender, LabelEditEventArgs e)
		{
			if (e.Label == null)
			{
				return;
			}
			if (string.IsNullOrEmpty(e.Label.Trim()))
			{
				YJKMessageBox.Error("名称不能为空！");
				e.CancelEdit = true;
				return;
			}
			if (e.Label.IndexOfAny(new char[]
			{
				'|',
				':',
				','
			}) > -1)
			{
				YJKMessageBox.Error("名称中不能包含如下字符“|” “：” “，” !");
				e.CancelEdit = true;
				return;
			}
			foreach (object obj in this.listViewCombin.Items)
			{
				ListViewItem listViewItem = (ListViewItem)obj;
				if (listViewItem != this.listViewCombin.SelectedItems[0] && listViewItem.Text.Equals(e.Label))
				{
					YJKMessageBox.Error(string.Format("名称“{0}”已存在，请重新命名！", e.Label));
					e.CancelEdit = true;
					break;
				}
			}
		}

		private void btnChangeLayType(object sender, EventArgs e)
		{
			this.hyTextBox1.Enabled = !this.radSelPoint.Checked;
		}

		private const int LVM_FIRST = 4096;

		private const int LVM_SETICONSPACING = 4149;

		private string pipeValveTableName = "PipeValve";

		private Document doc;

		private int nValveNumMax;

		private List<ListItem> listFamilyName;

		public int nLayType = 1;
	}
}
